﻿using System;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Data;

namespace EasySL.Controls
{
    public class ExDataGridStringColumn : ExDataGridColumn
    {
        protected override IValueConverter Converter
        {
            get
            {
                return new ExStringConverter();
            }
        }

        public override ExFilterType FilterType
        {
            get
            {
                return ExFilterType.String;
            }
        }

        public override TextAlignment TextAlignment
        {
            get
            {
                return TextAlignment.Left;
            }
        }
    }

    public class ExDataGridNumberColumn : ExDataGridColumn
    {
        protected override IValueConverter Converter
        {
            get
            {
                return new ExNumberConverter();
            }
        }

        public override ExFilterType FilterType
        {
            get
            {
                return ExFilterType.Number;
            }
        }
    }

    public class ExDataGridFloatNumberColumn : ExDataGridColumn
    {
        protected override IValueConverter Converter
        {
            get
            {
                return new ExFloatNumberConverter();
            }
        }

        public override ExFilterType FilterType
        {
            get
            {
                return ExFilterType.Number;
            }
        }
    }


    public class ExDataGridPercentColumn : ExDataGridColumn
    {
        protected override IValueConverter Converter
        {
            get
            {
                return new ExPercentConverter();
            }
        }

        public override ExFilterType FilterType
        {
            get
            {
                return ExFilterType.Number;
            }
        }
    }

    public class ExDataGridCheckBoxColumn : ExDataGridColumn
    {
        public ExDataGridCheckBoxColumn()
            : base()
        {
            this.Width = new DataGridLength(25);
            this.Header = "i";
            this.BindProperty = "IsChecked";
        }

        protected override DataTemplate CreateDataTemplate(string value)
        {
            string converterName = this.Converter.GetType().Name;

            string xaml = string.Format(this.DataTemplate, value);

            DataTemplate exDataTemplate = (DataTemplate)XamlReader.Load(xaml);

            return exDataTemplate;
        }

        protected override string DataTemplate
        {
            get
            {
                return
                     "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
                        + "<CheckBox HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\" Margin=\"2\" IsChecked=\"{{Binding {0}"
                        + ", Mode=TwoWay}}\"/> </DataTemplate>";
            }
        }
    }

    public class ExDataGridLinkColumn : ExDataGridColumn
    {
        #region BindUrl
        /// <summary>
        /// Identifies the BindUrl dependency property.
        /// </summary>
        public static readonly DependencyProperty BindUrlProperty =
            DependencyProperty.Register("BindUrl",
                typeof(String),
                typeof(ExDataGridLinkColumn),
                null);

        /// <summary>
        /// Gets or sets BindUrl.
        /// </summary>
        public String BindUrl
        {
            get { return (String)GetValue(BindUrlProperty); }
            set { SetValue(BindUrlProperty, value); }
        }
        #endregion
        
        protected override IValueConverter Converter
        {
            get
            {
                return new ExStringConverter();
            }
        }

        public override ExFilterType FilterType
        {
            get
            {
                return ExFilterType.String;
            }
        }

        protected override string DataTemplate
        {
            get
            {
                return
                     "<DataTemplate xmlns:ExDataGrid=\"clr-namespace:EasySL.Controls;assembly=EasySL.Controls\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
                        + "<Grid Margin=\"2\">"
                        + "<Grid.ColumnDefinitions><ColumnDefinition Width=\"5\"/><ColumnDefinition/><ColumnDefinition Width=\"8\"/><ColumnDefinition Width=\"auto\"/></Grid.ColumnDefinitions>"
                        + "<ExDataGrid:ExLinkButton Grid.Column=\"1\" VerticalAlignment=\"Center\" Content=\"{{Binding {0}"
                        + ", Converter={{StaticResource {1}}}"
                        + ", ConverterParameter='{2}'}}\" "
                        + "  Url=\"{{Binding {0}"
                        + ", Converter={{StaticResource {1}}}"
                        + ", ConverterParameter='{4}'}}\""
                        + "/>"
                        + "<TextBlock Grid.Column=\"3\" FontSize=\"9\" VerticalAlignment=\"Center\" Foreground=\"#b9b9b9\" TextAlignment=\"Right\" Text=\"{{Binding {0}"
                        + ", Converter={{StaticResource ExNumberConverter}}"
                        + ", ConverterParameter='{3}'}}\"/> "
                        + "</Grid>"
                        + "</DataTemplate>";
            }
        }

        protected override DataTemplate CreateDataTemplate(string value)
        {
            string converterName = this.Converter.GetType().Name;

            this.AddConverterResource(converterName, this.Converter);

            string xaml = string.Format(this.DataTemplate, "Data", converterName, value, value + "_Order", this.BindUrl);

            DataTemplate exDataTemplate = (DataTemplate)XamlReader.Load(xaml);

            return exDataTemplate;
        }
    }
}
